package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import st.business.tepth.R;
import st.business.tepth.business.bean.GoodsBean;
import st.business.tepth.view.ClearEditText;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/2/18
 */
public class DialogGoodsChoose extends Dialog {
    public DialogGoodsChoose(Context context, final List<GoodsBean> list) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_goods_choose, null);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ClearEditText etSearch = view.findViewById(R.id.et_search);
        final RecyclerView recyclerViewSearch =
                view.findViewById(R.id.recycler_view_goods_result);
        recyclerViewSearch.setLayoutManager(new LinearLayoutManager(context));
        final SearchPartAdapter searchPartAdapter =
                new SearchPartAdapter(R.layout.item_part_search, null);
        searchPartAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) recyclerViewSearch.getParent());
        recyclerViewSearch.setAdapter(searchPartAdapter);
        recyclerViewSearch.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));

        //推荐的
        RecyclerView recyclerViewCommon = view.findViewById(R.id.recycler_view_common_goods);
        recyclerViewCommon.setLayoutManager(new LinearLayoutManager(context));
        recyclerViewCommon.setAdapter(new SearchPartAdapter(R.layout.item_part_search, list));
        recyclerViewCommon.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        Observer observable = RxTextView.textChanges(etSearch)
                .subscribeOn(AndroidSchedulers.mainThread())
                .debounce(600, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .filter(new Predicate<CharSequence>() {
                    @Override
                    public boolean test(CharSequence charSequence) throws Exception {
                        if (charSequence.length() == 0) {
                            recyclerViewSearch.setVisibility(View.GONE);
                        } else {
                            recyclerViewSearch.setVisibility(View.VISIBLE);
                        }
                        return charSequence.length() > 0;
                    }
                })
                .switchMap(new Function<CharSequence, Observable<List<GoodsBean>>>() {
                    @Override
                    public Observable<List<GoodsBean>> apply(CharSequence charSequence) throws Exception {
                        return Observable.create(new ObservableOnSubscribe<List<GoodsBean>>() {
                            @Override
                            public void subscribe(ObservableEmitter<List<GoodsBean>> emitter) throws Exception {
                                emitter.onNext(list);
                            }
                        });
                    }
                })
                .subscribeWith(new Observer<List<GoodsBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<GoodsBean> response) {
                        // recyclerViewResult.setVisibility(View.VISIBLE);
                        searchPartAdapter.setNewData(response);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        setContentView(view);
    }

    private class SearchPartAdapter extends BaseQuickAdapter<GoodsBean, BaseViewHolder> {

        SearchPartAdapter(int layoutResId, @Nullable List<GoodsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, GoodsBean item) {

            helper.setText(R.id.tv_part_name, item.getName());
            helper.setText(R.id.tv_part_type, "零部件:" + item.getCode());
            helper.setText(R.id.tv_brand, "品牌:" + item.getBrand());
            helper.setText(R.id.tv_version, "型号:" + item.getVersion());
            helper.setText(R.id.tv_specification, "规格:" + item.getSpecification());
            helper.setText(R.id.tv_batch, "批次:" + item.getBatch());
        }
    }
}
